package com.travelsky.airchina.boarding.web.validator;

import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

import com.travelsky.airchina.boarding.domain.NewsColumn;
import com.travelsky.airchina.boarding.service.NewsColumnService;
/**
 * 新闻栏目验证器
 * @author haichenwang
 *
 */
public class NewsColumnValidator {
	public void validate(NewsColumn newsColumn,NewsColumnService newsColumnService,String oldNewsColumnName,String oldNewsColumnCode, Errors errors) {
		String name = newsColumn.getName();
		if (!StringUtils.hasLength(name)) {
			errors.rejectValue("name", "required",new Object[]{"8Ŀ���"}, "required");
		} else if (newsColumn.getId()==null && newsColumnService.isNewsColumnNameExist(newsColumn.getName())) {
			errors.rejectValue("name", "duplicate", new Object[]{"8Ŀ���"},"already exists");
		} else if (newsColumn.getId()!=null) {
			if(!name.equals(oldNewsColumnName) && newsColumnService.isNewsColumnNameExist(newsColumn.getName())){
				errors.rejectValue("name", "duplicate", new Object[]{"8Ŀ���"},"already exists");
			}
		}
		if (!StringUtils.hasLength(newsColumn.getCode())) {
			errors.rejectValue("code", "required",new Object[]{"8Ŀ����"}, "required");
		} else if (newsColumn.getId()==null && newsColumnService.isNewsColumnCodeExist(newsColumn.getCode())) {
			errors.rejectValue("code", "duplicate", new Object[]{"8Ŀ����"},"already exists");
		} else if (newsColumn.getId()!=null){
			if(!newsColumn.getCode().equals(oldNewsColumnCode) && newsColumnService.isNewsColumnCodeExist(newsColumn.getCode())){
				errors.rejectValue("code", "duplicate", new Object[]{"8Ŀ����"},"already exists");
			}
		}
			
	}
}
